﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using NUS.ISS.SRKBS.UI.Models;

using NUS.ISS.SRKBS.BLL;

namespace NUS.ISS.SRKBS.UI.Controllers
{
    public class RegulatoryController : Controller
    {
        //
        // GET: /Regulatory/

        public ActionResult Index()
        {
            RegulatoryRuleEngine.Init(); 
            return View();
        }

        [HttpPost]
        public ActionResult RegulatoryRuleTrigger(string ProductId)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    ClipState clipState = (ClipState) RegulatoryRuleEngine.GetState();
                    if (clipState.state.Equals("initial"))
                    {
                        RegulatoryRuleEngine.NextState();
                        clipState = (ClipState)RegulatoryRuleEngine.GetState();
                    }

                    if (clipState.state.Equals("middle"))
                    {
                        // Assert 
                        List<RegulatoryAttribute> strAttribures = new List<RegulatoryAttribute>();  
                        strAttribures.Add(new RegulatoryAttribute() { AttributeName = "project", AttributeValue = "AbCD", IsCLIPSNotifyRequired = true, IsString = true });
                        strAttribures.Add(new RegulatoryAttribute() { AttributeName = "product", AttributeValue = ProductId, IsCLIPSNotifyRequired = true, IsString = true });
                        RegulatoryRuleEngine.AssertAttributes(strAttribures);

                        // Next state
                        RegulatoryRuleEngine.NextState();
                        clipState = (ClipState)RegulatoryRuleEngine.GetState();                        
                    }

                    if (clipState.state.Equals("final"))
                    {
                        ViewBag.Result = "Not Implemented Yet.";
                        return PartialView("_RegulatoryNoRule");
                    }
                    else
                    {
                        ViewBag.DisplayForm = "Y";
                        return PartialView("_RegulatoryRulePartial");
                    }                    
                }
                else
                {
                    return View();
                } 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return View();
            }
        }

        [HttpPost]
        public ActionResult RegulatoryRuleTriggerAuto(string attrName, string attrValue)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    ClipState clipState = (ClipState)RegulatoryRuleEngine.GetState();
                    if (clipState.state.Equals("middle"))
                    {
                        // Assert 
                        List<RegulatoryAttribute> strAttribures = new List<RegulatoryAttribute>();
                        strAttribures.Add(new RegulatoryAttribute() { AttributeName = attrName, AttributeValue = attrValue, IsCLIPSNotifyRequired = true, IsString = true });
                        RegulatoryRuleEngine.AssertAttributes(strAttribures);

                        // Next state
                        RegulatoryRuleEngine.NextState();
                        clipState = (ClipState)RegulatoryRuleEngine.GetState();
                    }

                    if (clipState.state.Equals("final"))
                    {
                        ViewBag.Result = "Not Implemented Yet.";
                        if (clipState.display.Equals("CheckWithRegulatoryManager"))
                        {
                            ViewBag.Result = "Please check with Regulatory Manager.";
                        }
                        return PartialView("_RegulatoryNoRule");
                    }
                    else
                    {
                        ViewBag.DisplayForm = "Y";
                        if (clipState.display.Equals("ProductActive"))
                        {
                            return PartialView("_RegulatoryRuleActive");
                        }
                        else if (clipState.display.Equals("ProductActiveWireless"))
                        {
                            return PartialView("_ProductActiveWireless");
                        }
                        else if (clipState.display.Equals("ProductActiveWired"))
                        {
                            return PartialView("_ProductActiveWired");
                        }
                        else if (clipState.display.Equals("PowerSupplyBattery"))
                        {
                            return PartialView("_BatterySelection");
                        }
                        else if (clipState.display.Equals("BatteryTypePrimary"))
                        {
                            return PartialView("_BatteryPrimary");
                        }
                        else if (clipState.display.Equals("BatteryTypeSecondary"))
                        {
                            return PartialView("_BatterySecondary");
                        }
                        else
                        {
                            return View();
                        }
                    }                   
                }
                else
                {
                    return View();
                }
                
            }
            catch (Exception e)
            {
                return View();
            }
        }

    }
}
